<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\ClcwUser;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Common\PhpRedis;

class UserExchange
{
    private $userExchangeModel;
    private $redis;

    public function __construct()
    {
        $this->userExchangeModel = M('UserExchange', 'clcw_', 'DB_CLCW');
        $this->redis = new PhpRedis(0);
    }

    /**
     *  兑换积分订单管理
     *
     * @param int $page
     * @param int $size
     * @param     $params
     */
    public function getUserExchangeList($page = 1, $size = 10, $params)
    {
        $map = [];
        $map['status'] = ['eq', $params['status']];
        if ($params['order_no']) {
            $map['order_sn'] = ['LIKE', '%' . $params['order_no'] . '%'];
        }
        if ($params['user_id']) {
            $map['user_id'] = ['eq', $params['user_id']];
        }
        if ($params['user_name']) {
            $uc = new ClcwUser();
            $user_id = $uc->getUserIdByNickName($params['user_name']);
            $map['user_id'] = ['eq', $user_id];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        if ($params['status'] == 2) {
            if ($params['return_start_time']) {
                $map['return_time'] = ['egt', strtotime($params['return_start_time'] . ' 00:00:00')];
            }
            if ($params['return_end_time']) {
                $map['return_time'] = ['elt', strtotime($params['return_end_time'] . ' 23:59:59')];
            }
            if ($params['return_start_time'] && $params['return_end_time']) {
                $map['return_time'] = [
                    ['egt', strtotime($params['return_start_time'] . ' 00:00:00')],
                    ['elt', strtotime($params['return_end_time'] . ' 23:59:59')]
                ];
            }
            $order = 'return_time DESC';
        } else {
            $order = 'create_time DESC';
        }
        $cols = 'id,order_sn,duiba_sn,user_id,score,create_time,status,return_time';
        $total = $this->userExchangeModel->field($cols)
            ->where($map)->count();
        $list = $this->userExchangeModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
//        echo $this->userExchangeModel->_sql();
        if (!empty($list)) {
            $uc = new ClcwUser();
            foreach ($list as $k => $v) {
                $userInfo = $uc->getClcwUserInfo($v['user_id']);
                $list[$k]['user_name'] = $userInfo['nick_name'];
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
                $list[$k]['return_time'] = date("Y-m-d H:i:s", $v['return_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }

    /**
     * 兑换积分订单详情
     *
     * @param $id
     *
     * @return array
     */
    public function getUserExchangeInfo($id)
    {
        $condition['id'] = ['eq', $id];
        $cols = 'id,order_sn,score,user_id,create_time,status,emp_id,return_time';
        $res = $this->userExchangeModel->field($cols)->where($condition)->find();
        $uc = new ClcwUser();
        $userInfo = $uc->getClcwUserInfo($res['user_id']);
        $res['user_name'] = $userInfo['nick_name'];
        $res['create_time'] = date("Y-m-d H:i:s", $res['create_time']);
        $employee = Employee::user_info_id($res['emp_id']);
        $res['emp_name'] = $employee['real_name'];
        $res['return_time'] = date("Y-m-d H:i:s", $res['return_time']);
        return $res;
    }

    /**
     * 处理兑换积分订单退单操作
     *
     * @param array $data
     * @param int   $user
     */
    public function returnUserExchangeOrder($data = [], $user)
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        //更新兑换订单信息
        $uc = new ClcwUser();
        M('', 'clcw_', 'DB_CLCW')->startTrans();
        $ret1 = $this->userExchangeModel->save($data);
//        $ret2 = $uc->change_user_score($user['user_id'], 49, '退回兑换商品积分', $user['score']);
        $ret2 = $this->redis->lpush(C('REDIS_KEY')['biz_queue'], json_encode(
                [
                    'bizContent' =>
                        [
                            'scoreType' => 49,
                            'score'     => (int)$user['score'],
//                            'remark' => '退回兑换商品积分',
                            'remark'    => '',
                            'userId'    => (int)$user['user_id']
                        ],
                    'bizType'    => 5
                ]
            )
        );
        if (!$ret1 || !$ret2) {
            M('', '', 'DB_CLCW')->rollback();
            return false;
        }
        M('', 'clcw_', 'DB_CLCW')->commit();
        return true;
    }
}